import React from "react";
import { Button, Popconfirm, Icon, Tooltip, Divider } from "antd";

import { ButtonLink } from "./Styled";
import useAuth from "../hooks/useAuth";
import { checkAuth } from "../utils/checkAuth";

export const AuthCreate = ({ auth, handleCreate }) => {
  const { loginUser } = useAuth();
  return checkAuth({ auth, loginUser }) ? (
    <Button type="primary" onClick={handleCreate}>
      新建
    </Button>
  ) : null;
};

export const AuthCreateSub = ({ auth, record, handleCreateSub }) => {
  const { loginUser } = useAuth();
  return checkAuth({ auth, loginUser }) ? (
    <>
      <Tooltip title={`新建子权限`}>
        <ButtonLink
          onClick={() => {
            handleCreateSub(record);
          }}
        >
          <Icon type="file-add" />
        </ButtonLink>
      </Tooltip>
      <Divider type="vertical" />
    </>
  ) : null;
};

export const AuthDelete = ({ auth, record, handleDelete }) => {
  const { loginUser } = useAuth();
  return checkAuth({ auth, loginUser }) ? (
    <Popconfirm
      title="确认删除吗？"
      onConfirm={() => {
        handleDelete(record.id);
      }}
      okText="确定"
      cancelText="取消"
    >
      <Tooltip title={`删除`}>
        <ButtonLink>
          <Icon type="delete" />
        </ButtonLink>
      </Tooltip>
    </Popconfirm>
  ) : null;
};

export const AuthEdit = ({ auth, record, handleEdit }) => {
  const { loginUser } = useAuth();
  return checkAuth({ auth, loginUser }) ? (
    <>
      <Tooltip title={`编辑`}>
        <ButtonLink
          onClick={() => {
            handleEdit(record);
          }}
        >
          <Icon type="form" />
        </ButtonLink>
      </Tooltip>
      <Divider type="vertical" />
    </>
  ) : null;
};
